<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class T_chat extends MY_Model{
	public function __construct(){
		parent::__construct();
		$this->table_name = T_CHATS;
	}

	function getChatLog($select , $where = -1 , $limit_size = 0 , $limit_from){
		if($limit_size != 0){
			if($limit_from != 0){
				$this->db->limit($limit_from , $limit_from + $limit_size);
			}else{
				$this->db->limit(50);
			}
		}

		if( is_array($where)){
			$this->db->where($where);
		}else if(is_numeric($where)){
			if($where > 0){
				$this->db->where(CHAT_ID,$where);
			}else{
				return -1;
			}
		}else if(is_string($where)){
			$this->db->where($where);
		}else{
			return -1;	
		}
		$this->db->order_by(CHAT_CREATE_DATE, "DESC");
		$this->db->select($select);

		$result = $this->db->get($this->table_name);

		$result = $result->result_array();
		
		return $result;

	}

	public function insertChatLog($array){
		if(!isset($array[CHAT_CONTENT]) || !isset($array[CHAT_TO]) || !isset($array[CHAT_TYPE])){
			return -1;
		}

		$this->db->insert($this->table_name , $array);
		
		//get last id
		$id_last = $this->db->insert_id();

		return $id_last;
	}

	public function getChatLogSegment($where = array(), $per_page = 50, $page = 0){
		if($where == null || !is_array($where)){
			return null;
		}

		$this->db->where($where);
		$this->db->order_by(CHAT_CREATE_DATE, "DESC");
		$this->db->limit($per_page, $page*$per_page);
		$result = $this->db->get($this->table_name);
		$result = $result->result_array();
		return $result;
	}

	/**
	 * get user by id
	 * @param  string  $select : fields of table need get
	 * @param  integer $id     id of user
	 * @return array|null
	 */
	public function get_data_by_id($id = -1, $select = '*'){
		if($id == -1 || !is_numeric($id)){
			return null;
		}

		if(strcmp($select, '*') == 0){
			$this->db->select();
		}else{
			$this->db->select($select);
		}

		$this->db->where(CHAT_ID ." = $id LIMIT 1");

		$result = $this->db->get($this->table_name);
		$result = $result->result_array();
		return $result;
	}

	/**
	 * get data by property
	 * @param  string $select : fields of table need get
	 * @param  array  $where  conditions
	 * @return array|null
	 */
	public function get_data_by_property($where = array(), $select = '*'){
		if($where == null || !is_array($where)){
			return null;
		}

		// query
		if(strcmp($select, '*') == 0){
			$this->db->select();
		}else{
			$this->db->select($select);
		}
		$this->db->where($where);
		$result = $this->db->get($this->table_name);
		$result = $result->result_array();
		return $result;
	}

	/**
	 * insert data into talbe
	 * @param array   $data   array data insert
	 * @param integer $result if 0 return id inserted else 1 return data user inserted
	 * @return null|integer|array
	 */
	public function set_data($data = array(), $result = 0){
		if($data == null || !is_array($data)){
			return null;
		}

		// insert rows
		$this->db->insert($this->table_name, $data);
		// get id
		$last_id = $this->db->insert_id();
		//return
		if($result == 0){
			return $last_id;
		}else{
			return $this->get_data_by_id($last_id);
		}
	}

	/**
	 * update data by id
	 * @param  array   $data : data update
	 * @param  integer $id
	 * @return integer
	 */
	public function update_data_by_id($data = array(), $id = -1){
		if($data == null || !is_array($data) || $id == -1 || !is_numeric($id)){
			return null;
		}

		//condition
		$this->db->where(CHAT_ID ." = $id LIMIT 1");
		$this->db->update($this->table_name, $data);
		return $this->db->affected_rows();
	}

	/**
	 * update data
	 * @param  array  $data
	 * @param  array  $where : conditions update
	 * @return integer
	 */
	public function update_data_by_property($data = array(), $where = array()){
		if($data == null || !is_array($data) || $where == null || !is_array($where)){
			return null;
		}

		$this->db->where($where);
		$this->db->update($this->table_name, $data);
		return $this->db->affected_rows();
	}
}